package com.sunthy.mall.service.admin.admin;

import com.sunthy.mall.dao.admin.AdminDao;
import com.sunthy.mall.model.admin.bo.admin.AdminChangePwd;
import com.sunthy.mall.model.admin.pojo.admin.Admin;
import com.sunthy.mall.model.admin.bo.admin.AdminAddBo;
import com.sunthy.mall.model.admin.bo.admin.AdminLoginBo;
import com.sunthy.mall.model.admin.bo.admin.AdminSearchBo;
import com.sunthy.mall.model.admin.vo.admin.AllAdminVO;
import com.sunthy.mall.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: XM
 */
public class AdminServiceImpl implements AdminService {

    // 通过mybatis动态代理 查询数据库中是否有AdminLoginBo对象
    // 从而返回一个200(有) 404(没有) 状态码
    public int login(AdminLoginBo loginBO) {
        // 需要读取mybatis.xml文件
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        Admin admin = new Admin(null, loginBO.getEmail(), loginBO.getPwd(), null);
        int count = adminDao.count(admin);
        // 即便是查询,也要把sqlSession给关闭,否则会出现死锁问题
        // sqlSession一定不要写成成员变量,不能复用
        sqlSession.commit();
        sqlSession.close();
        if (count == 1) {
            return 200;
        }
        return 404;
    }


    /**
     * 返回所有用户信息
     *
     * @return
     */
    public List<AllAdminVO> allAdmins() {
        // sqlSession一定不能设置成成员变量
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);

        List<Admin> adminList = adminDao.allAdmins(new Admin());
        sqlSession.commit();
        sqlSession.close();
        return getAllAdminVOSList(adminList);
    }

    /**
     * 向数据库中添加用户
     * 返回状态码
     *
     * @param adminAddBo
     * @return
     */
    public int add(AdminAddBo adminAddBo) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        Admin admin = new Admin(null, adminAddBo.getEmail(), adminAddBo.getPwd(), adminAddBo.getNickname());
        // 首先 查询有无重复
        int countSel = adminDao.addSel(admin);
        if (countSel != 0) { // 重复 返回10000
            sqlSession.commit();
            sqlSession.close();
            return 10000;
        }
        // 向数据库中添加 返回0
        adminDao.add(admin);
        sqlSession.commit();
        sqlSession.close();
        return 0;
    }

    /**
     * 通过id查找数据库中的admin
     *
     * @param id
     */
    public Admin getAdminInfo(Integer id) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        Admin adminInfo = adminDao.info(id);
        sqlSession.commit();
        sqlSession.close();
        return adminInfo;
    }

    /**
     * 通过回显返回的AlladminVo对象去数据库中进行修改
     * 先按判断有无修改 在删除 在添加
     *
     * @param allAdminVO
     * @return
     */
    public int update(AllAdminVO allAdminVO) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);

        // 去数据库中 查询相同username大的个数
        int addSel = adminDao.addSel(new Admin(null, allAdminVO.getEmail(), null, null));
        if (addSel > 1) {// 说明有两个 一样username的admin 返回10000
            sqlSession.close();
            return 10000;
        } else if (addSel == 1) {//只有一个 那么判断是不是本身的
            Admin info = adminDao.info(allAdminVO.getId());
            if (!info.getUsername().equals(allAdminVO.getEmail())) {// 不是本身的  那么返回10000
                sqlSession.close();
                return 10000;
            }
        }
        // 修改
        adminDao.update(new Admin(allAdminVO.getId(), allAdminVO.getEmail(), allAdminVO.getPwd(), allAdminVO.getNickname()));
        sqlSession.commit();
        sqlSession.close();
        return 0;
    }

    /**
     * 通过id删除admin
     *
     * @param id
     * @return
     */
    public int delete(int id) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);

        // 去数据库中删除
        int delete = adminDao.delete(id);
        sqlSession.commit();
        sqlSession.close();
        if (delete != 1) {
            return 10000;
        }
        return 0;
    }

    /**
     * 通过adminsearchbo对象查找所有符合要求的admin
     *
     * @return
     */
    public List<AllAdminVO> searAllAdmins(AdminSearchBo adminSearchBo) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        // 去数据库中查询
        List<Admin> adminList = adminDao.allAdmins(new Admin(null, adminSearchBo.getEmail(), null,
                adminSearchBo.getNickname()));
        sqlSession.commit();
        sqlSession.close();

        return getAllAdminVOSList(adminList);

    }

    /**
     * 修改密码的业务逻辑
     * @param adminChangePwd
     * @return int
     */
    @Override
    public int updatePwd(AdminChangePwd adminChangePwd) {
        SqlSession sqlSession = MybatisUtils.openSession();
        AdminDao adminDao = sqlSession.getMapper(AdminDao.class);
        // 修改密码
        // 查询旧密码是否则正确
        List<Admin> adminList = adminDao.allAdmins(new Admin(null, adminChangePwd.getAdminToken(), adminChangePwd.getOldPwd(),
               null ));
        if (adminList.size()!=1){
            sqlSession.commit();
            sqlSession.close();
            return 500;
        }
        // 修改
        adminDao.updatePwd(adminChangePwd);
        sqlSession.commit();
        sqlSession.close();
        return 0;
    }

    /**
     * 将返回的AdminList 封装成 AllAdminVoList
     *
     * @param adminList
     * @return
     */
    private List<AllAdminVO> getAllAdminVOSList(List<Admin> adminList) {
        List<AllAdminVO> adminVOS = new ArrayList<AllAdminVO>();
        for (Admin admin : adminList) {
            AllAdminVO adminVO = new AllAdminVO(admin.getId(), admin.getUsername(), admin.getPassword(),
                    admin.getNickname());
            adminVOS.add(adminVO);
        }
        return adminVOS;
    }
}
